(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.role.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'roleService' ];
	function deleteController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, roleService) {
		var ctrl = this, roleInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.role={};
		ctrl.init = function() {
			ctrl.roleRightGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.roleRight.tableConfigs.table, rowSelectionChanged);
			ctrl.roleRightGridOptions.data = 'vm.role.roleRights';
			ctrl.roleRightGridOptions.enableRowHeaderSelection = false;
			function rowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedDTParmList = selectedRows;
			}
			authService.delete(roleInfo).then(function(response) {
				ctrl.role = response.data || roleInfo;
				ctrl.role.dataStatus = ctrl.role.dataStatus || 'V';
				ctrl.role.roleRights = ctrl.role.roleRights ||[];
				angular.forEach(ctrl.role.roleRights, function (con, key){
					con.dataStatus = ctrl.role.dataStatus;
				});
			});
		};
		
		
		ctrl.deleteSubmit = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.role.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.role).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.role.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.role.list',
					'state' : 'cda.role.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);